import 'package:connectfour/record.dart';
import 'package:flutter/material.dart';
import 'package:connectfour/datas.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'game_page.dart';

class WelcomePage extends StatefulWidget {
  const WelcomePage({
    super.key,
  });

  @override
  State<WelcomePage> createState() => _WelcomePageState();
}

class _WelcomePageState extends State<WelcomePage> {
  final _Controller1 = TextEditingController();
  final _Controller2 = TextEditingController();

  // Future saveString() async {
  //   print('存入');
  //   final prefs = await SharedPreferences.getInstance();
  //
  //   await prefs.setString('player1', player1);
  //   await prefs.setBool('isTime', isTime);
  // }

  Future loadString() async {
    print('读取');
    final prefs = await SharedPreferences.getInstance();
    // isTime = prefs.getBool('isTime') ?? false;
    player1 = prefs.getString('player1') ?? '';
    player2 = prefs.getString('player2') ?? '';
    // time = prefs.getString('min') ?? '00';
    // time = prefs.getString('time') ?? '00 : 00';
    isStart = prefs.getBool('isStart') ?? false;
    if (isStart) {
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => GamePage()));
    }
    setState(() {});
  }

  @override
  void initState() {
    // TODO: implement initState
    loadString();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: SingleChildScrollView(
          child: Container(
            height: 600,
            margin: EdgeInsets.symmetric(vertical: 20),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  'Connect Four',
                  style: TextStyle(
                      fontSize: 36,
                      color: Colors.black,
                      fontWeight: FontWeight.w800),
                ),
                Container(
                  height: 80,
                  width: 600,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(30)),
                      border: Border.all(width: 1, color: Colors.black)),
                  child: Center(
                    child: TextField(
                      textAlign: TextAlign.center,
                      controller: _Controller1,
                      cursorColor: Colors.black,
                      decoration: const InputDecoration(
                        border: InputBorder.none,
                        hintText: '玩家1名称',
                      ),
                    ),
                  ),
                ),
                Container(
                  height: 80,
                  width: 600,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(30)),
                      border: Border.all(width: 1, color: Colors.black)),
                  child: Center(
                    child: TextField(
                      textAlign: TextAlign.center,
                      controller: _Controller2,
                      cursorColor: Colors.black,
                      decoration: const InputDecoration(
                        border: InputBorder.none,
                        hintText: '玩家2名称',
                      ),
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    if (_Controller1.text == '' || _Controller2.text == '') {
                      print('玩家姓名不能为空');
                      showDialog(
                          context: context,
                          builder: (BuildContext context) {
                            return AlertDialog(
                              title: const Text('提示'),
                              content: const Text(
                                '玩家姓名不能为空',
                                style: TextStyle(fontSize: 18),
                              ),
                              actions: [
                                TextButton(
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                    },
                                    child: const Text('关闭'))
                              ],
                            );
                          });
                    } else {
                      player1 = _Controller1.text;
                      player2 = _Controller2.text;
                      Navigator.push(context,
                          MaterialPageRoute(builder: (context) {
                        return const GamePage();
                      }));
                    }
                  },
                  child: Container(
                    width: 550,
                    height: 50,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(20)),
                      color: Colors.grey[200],
                    ),
                    child: Center(
                        child: Text(
                      'Start Game',
                      style: TextStyle(fontSize: 20),
                    )),
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    Navigator.push(context,
                        MaterialPageRoute(builder: (context) {
                      return RecordPage();
                    }));
                  },
                  child: Container(
                    width: 550,
                    height: 50,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(20)),
                      color: Colors.grey[200],
                    ),
                    child: Center(
                        child: Text(
                      'Record',
                      style: TextStyle(fontSize: 20),
                    )),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
